import '.env'
import common from "./common.js";
// import store from "../store/index.js";
let host, post, imgUrl, apiUrl;
let BASE_API = process.uniEnv.BASE_API;
let BASE_HOST = process.uniEnv.BASE_HOST;
let BASE_IMG = process.uniEnv.BASE_IMG_URL;
host = BASE_API;
post = BASE_HOST;
var imgBeforeUrl = "/static/images/" //图片相对路径
// var apiBeforeUrl = "/api" //api接口路径
var apiBeforeUrl = "" //api接口路径
// console.log(process.uniEnv);

function getApiUrl() {
    return host
    // if (post == '') {
    //
    //     return host + apiBeforeUrl
    // } else {
    //     return host + ":" + post + apiBeforeUrl
    // }
}

function getImageUrl() {
    if (post == '') {
        return BASE_IMG + imgBeforeUrl
    } else {
        return BASE_IMG + ":" + post + imgBeforeUrl
    }
}

imgUrl = getImageUrl()
apiUrl = getApiUrl()
// console.log("接口地址为:", apiUrl);
// console.log("图片地址为:", imgUrl);
// const appName = store.state.config.shop_info_store_name
const appName = 'ggggggty'
    /*延时获取token*/
function getToken(url) {
    const token_type = "bearer";
    let token
    if (url == "/mall/logout") {
        // 退出登录不需要 token_type
        // token = store.state.token;;
				token = 'ttttttttttttoken';
        token = token.split(token_type)[1]

        // console.log(token)

    } else {
        // token = store.state.token;
				token = 'ttttttttttttoken';
        // console.log(token)
    }
    return token;
}

export function request(url, method, data, header = {}) {
    // console.log(url)
    return new Promise((resolve, reject) => {
        // uni.showLoading({
        //     title:'加载中'
        // })
        uni.request({
            url: apiUrl + url,
            data,
            method,
            header: {
                'content-type': 'application/json', // 默认值
                'Authorization': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjoiNmE3YTRhMzEtMmU2ZC00ZThkLWFiOTgtNjNkOTBjZDIyODRhIiwicm9sZSI6MTAsImlhdCI6MTY5NTYzMjA1M30.pgiRND5daNzl-cCA-WV2Q0eYZOXNXwLlYijFasFhNJQ',
                'lang': "langlanglang"
                // 'lang': store.state.lang
            },
            success: function(res) {
                // uni.hideLoading();
                if (res.statusCode != 200) {
                    if (res.statusCode == 400 || res.statusCode == 401 || res.statusCode == 402) { //token错误 清除登录信息
                        //重新登录
                        console.log('请求', url)
                        console.log('信息', res)
                            // store.state.hasLogin=false;
                            // store.state.user="";
                        common.toast("none", "重新登录", 3000)
                        setTimeout(() => {
                                const hotelSearchObj  = uni.getStorageSync("searchObj")
                                uni.clearStorage()
                                uni.clearStorageSync()
																// store.commit("getLogout")
																common.islogin()
                                if (hotelSearchObj) {
                                    uni.setStorageSync("searchObj", hotelSearchObj)
                                }
                            }, 3000)
                            // reject(res)
                        return false
                    }

                    if (res.statusCode == 501) {
                        var showMsg = (typeof res.data.msg == 'string') ? res.data.msg : '数据格式错误';
                        showError(`${showMsg}`, false)
                        reject(res)
                        return false
                    }
                    var showMsg = (typeof res.data.msg == 'string') ? res.data.msg : '数据格式错误';
                    showError(`${showMsg}`, false)
                    reject(res)
                    return false


                } else {
                    if (res.data.status == -10000) {
                        const hotelSearchObj  = uni.getStorageSync("searchObj")
                        /*清除所有缓存*/
                        // store.commit("getLogout")
                        uni.clearStorageSync()
                        uni.clearStorage()
                        if (hotelSearchObj) {
                            uni.setStorageSync("searchObj", hotelSearchObj)
                        }
                        common.toast("none", "身份信息失效重新登录", 3000)
                        setTimeout(() => {
                            common.islogin()
                        }, 3000)

                        return
                    }
                    // 未设置支付密码
                    if (res.data.status == -10020) {
                        let time = 3000
                        common.toast("none", "请先设置支付密码", time)
                        let url = "/pages/user/userinfo_edit/userinfo_edit?type=paypwd";
                        navigate(url, time)
                        return
                    }
                    /*短信验证 错误验证  改手机号不存在*/
                    if (res.data.status == -10010) {
                        resolve(res.data)
                        return false
                    }
                    //-10008商品是够支持配送
                    if (res.data.status == -10008) {
                        resolve(res.data)
                        return false
                    }
                    if (res.data.status < 1 && res.data.msg) {
						if (res.data.result && res.data.result.name) {
							showError(res.data.result.name, false)
						}else{
							showError(res.data.msg, false)
						}
                        reject(res)
                        return false;
                    }

                    resolve(res.data)
                }



            },
            fail: function(res) {},
            complete: function() {
                // uni.hideLoading()
            }
        })
    })
}

 function showError(content, isBack) {
    uni.showModal({
        title: "",
        content: content,
        showCancel: false,
        success: function(res) {
            if (res.confirm) {
                // console.log('用户点击确定');
                if (isBack == true) {
                    uni.navigateBack({
                        delta: 1
                    })
                }
            } else if (res.cancel) {
                // console.log('用户点击取消');
            }
        }
    });
}

function navigate(url, time) {
    setTimeout(() => {
        uni.navigateTo({
            url: url,
        })
    }, time)
}
host = apiUrl
export default {
    host,
    appName,
    imgUrl
}
